package view.maze;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Decorations;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

import view.maze.MazeView;;

public class MazeMenu {

	private Decorations parent;
	private MazeView ui;
	private Menu menuBar;
	private MenuItem cascadedFileMenu;
	private MenuItem cascadedEditMenu;
	private Menu fileMenu;
	private Menu editMenu;
	private MenuItem saveItem;
	private MenuItem loadItem;
	private MenuItem exitItem;
	private MenuItem undoItem;
	private MenuItem restartItem;
	
	MazeMenu(MazeView view, Decorations parent)
	{
	this.parent = parent;
	this.ui = view;

	// Create the menu bar
	menuBar = new Menu(parent, SWT.BAR);

	// Create the File Menu
	cascadedFileMenu = new MenuItem(menuBar, SWT.CASCADE);
	cascadedFileMenu.setText("&File");
	fileMenu = new Menu(parent, SWT.DROP_DOWN);
	fileMenu.setOrientation(SWT.LEFT_TO_RIGHT);
	cascadedFileMenu.setMenu(fileMenu);

	// Create the Edit Menu
	cascadedEditMenu = new MenuItem(menuBar, SWT.CASCADE);
	cascadedEditMenu.setText("&Edit");
	editMenu = new Menu(parent, SWT.DROP_DOWN);
	editMenu.setOrientation(SWT.LEFT_TO_RIGHT);
	cascadedEditMenu.setMenu(editMenu);

	// Create the Save menu item under the file menu
	saveItem = new MenuItem(fileMenu, SWT.PUSH);
	saveItem.setText("&Save\tCTRL+S");
	saveItem.setAccelerator(SWT.CTRL + 'S');

	// Create the Load menu item under the file menu
	loadItem = new MenuItem(fileMenu, SWT.PUSH);
	loadItem.setText("&Load\tCTRL+L");
	loadItem.setAccelerator(SWT.CTRL + 'L');

	// Create the exit menu item under the file menu
	exitItem  = new MenuItem(fileMenu, SWT.PUSH);
	exitItem.setText("&Exit\tALT+F4");
	exitItem.setAccelerator(SWT.ALT + SWT.F4);

	// Create the Undo menu item under the edit menu
	undoItem = new MenuItem(editMenu, SWT.PUSH);
	undoItem.setText("&Undo\tCTRL+Z");
	undoItem.setAccelerator(SWT.CTRL + 'Z');

	// Create the Restart menu item under the edit menu
	restartItem = new MenuItem(editMenu, SWT.PUSH);
	restartItem.setText("&Restart\tCTRL+N");
	restartItem.setAccelerator(SWT.CTRL + 'N');

	addListeners();
	}
	private void addListeners()
	{
		saveItem.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) 
			{
				ui.setUserCommand(MazeView.ACTION_SAVE);
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) 
			{ /* DO NOTHING */ }
		});

		loadItem.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) 
			{
				ui.setUserCommand(MazeView.ACTION_LOAD);
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) 
			{ /* DO NOTHING */ }
		});

		exitItem.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) 
			{
				MessageBox messageBox = new MessageBox((Shell)parent, SWT.ICON_QUESTION | SWT.YES | SWT.NO);
				messageBox.setText("Exiting Game");
				messageBox.setMessage("Are you sure you want to exit?");

				int response = messageBox.open();

				if (response == SWT.YES)
				{
					parent.getDisplay().dispose();
					System.exit(0);	
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) 
			{ /* DO NOTHING */ }
		});

		undoItem.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) 
			{
				ui.setUserCommand(MazeView.ACTION_UNDO);
				ui.getBoard().setFocus();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) 
			{ /* DO NOTHING */ }
		});

		restartItem.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) 
			{
				ui.setUserCommand(MazeView.ACTION_START_RESET);
				ui.getBoard().setFocus();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) 
			{ /* DO NOTHING */ }
		});
	}

	public Menu getMenuBar()
	{
		return this.menuBar;
	}


}

